﻿unit MonitorFrm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  TCPEngine, TCPListener, GlobalMonitor, Vcl.Controls, Vcl.StdCtrls,
  Vcl.ExtCtrls, Forms;

type
  TFrmMonitor = class(TForm)
    Panel1: TPanel;
    p1: TPanel;
    Label7: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label17: TLabel;
    Label15: TLabel;
    Label24: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Label34: TLabel;
    Label1: TLabel;
    Label6: TLabel;
    LB_NumberOfActiveConn: TLabel;
    LB_IocpAcceptExCount: TLabel;
    LB_RecvCount: TLabel;
    LB_IocpRecvCount: TLabel;
    LB_SendRate: TLabel;
    LB_NewBufferCount: TLabel;
    LB_TotalRecv: TLabel;
    LB_TotalSend: TLabel;
    LB_TimeRun: TLabel;
    LB_SendCount: TLabel;
    LB_IocpSendCount: TLabel;
    LB_RecvRate: TLabel;
    LB_FreeBufferCount: TLabel;
    Label8: TLabel;
    LB_BufferPoolCount: TLabel;
    LB_BufferMapCount: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    LB_HistoryCount: TLabel;
    Label4: TLabel;
    LB_HistoryTime: TLabel;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    _TCPEngine: TITCPEngine;
    _TCPListener: TITCPListener;
    procedure ClearLable;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); overload; override;
    constructor Create(AOwner: TComponent; ATCPEngine: TITCPEngine;
      ATCPListener: TITCPListener ); reintroduce; overload;
  end;

var
  FrmMonitor: TFrmMonitor;

implementation

{$R *.dfm}

{ TFrmMonitor }

constructor TFrmMonitor.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

constructor TFrmMonitor.Create(AOwner: TComponent; ATCPEngine: TITCPEngine;
  ATCPListener: TITCPListener);
begin
  inherited Create(AOwner);
  _TCPEngine := ATCPEngine;
  _TCPListener := ATCPListener;
  ClearLable;
end;

procedure TFrmMonitor.ClearLable;
begin
  LB_NumberOfActiveConn.Caption := '0';
  LB_BufferPoolCount.Caption := '0';
  LB_IocpAcceptExCount.Caption := '0';
  LB_IocpRecvCount.Caption := '0';
  LB_RecvCount.Caption := '0';
  LB_NewBufferCount.Caption := '0';
  LB_SendRate.Caption := '0';
  LB_TotalRecv.Caption := '0';
  LB_TotalSend.Caption := '0';
  LB_TimeRun.Caption := '0';
  LB_IocpSendCount.Caption := '0';
  LB_SendCount.Caption := '0';
  LB_FreeBufferCount.Caption := '0';
  LB_RecvRate.Caption := '0';
  LB_BufferMapCount.Caption := '0';
  LB_HistoryCount.Caption := '0';
  LB_HistoryTime.Caption := FormatDateTime('YYYY-MM-DD HH:MM:SS', Now());
end;

procedure TFrmMonitor.Timer1Timer(Sender: TObject);
var
  buf: array[0..1023] of char;
begin
  if not Assigned(_TCPEngine) then Exit;
  if not _GlobalMonitor.m_bStart then
  begin
    ClearLable;
    Exit;
  end;

  with _GlobalMonitor do
  begin
    _GlobalMonitor.CalcCapability;
    (* Socket连接数         *)
    FillChar(buf, 1024, 0);
    LB_NumberOfActiveConn.Caption := StrFmt(buf, ' %d ', [_TCPListener.FNumberOfActiveConnections]);

    (* 内存池Buffer个数     *)
    FillChar(buf, 1024, 0);
    LB_BufferPoolCount.Caption := StrFmt(buf, ' %d ', [_TCPEngine.MemoryPool.PoolBuffer.MaxNumberOfFreeBuffer]);

    (* 历史最大连接数       *)
    FillChar(buf, 1024, 0);
    LB_HistoryCount.Caption := StrFmt(buf, ' %d ', [m_MaxHistoryCount]);

    (* 工作线程             *)
    FillChar(buf, 1024, 0);
    LB_BufferMapCount.Caption := StrFmt(buf, ' %d ', [_TCPEngine.WorkThreadPool.ActiveThreads]);

    (* IOCP中ACCEPT个数     *)
    FillChar(buf, 1024, 0);
    LB_IocpAcceptExCount.Caption := StrFmt(buf, ' %d ', [_TCPListener.FInitPostAcceptEx]);

    (* IOCP队列中Recv数     *)
    FillChar(buf, 1024, 0);
    LB_IocpRecvCount.Caption := StrFmt(buf, ' %d ', [m_iocpRecvCount]);

    (* IOCP队列中Send数     *)
    FillChar(buf, 1024, 0);
    LB_IocpSendCount.Caption := StrFmt(buf, ' %d ', [m_iocpSendCount]);

    (* 投递Send总次数       *)
    FillChar(buf, 1024, 0);
    LB_SendCount.Caption := StrFmt(buf, ' %d ', [m_SendCount]);

    (* 投递Recv总次数       *)
    FillChar(buf, 1024, 0);
    LB_RecvCount.Caption := StrFmt(buf, ' %d ', [m_RecvCount]);

    (* 物理申请Buffer次数   *)
    FillChar(buf, 1024, 0);
    LB_NewBufferCount.Caption := StrFmt(buf, ' %d ', [m_iNewBufferCount]);

    (* 物理释放Buffer次数   *)
    FillChar(buf, 1024, 0);
    LB_FreeBufferCount.Caption := StrFmt(buf, ' %d ', [m_iFreeBufferCount]);

    (* 接受字节数           *)
    FillChar(buf, 1024, 0);
    LB_TotalRecv.Caption := StrFmt(buf, ' %d  TB  %d  GB  %4d  MB  %4d  KB  %4d  Byte ',
    [m_nTotalRecvTB, m_nTotalRecvGB, m_nTotalRecvMB, m_nTotalRecvKB, m_nTotalRecvBytes ]);

    (* 发送字节数           *)
    FillChar(buf, 1024, 0);
    LB_TotalSend.Caption := StrFmt(buf, ' %d  TB  %d  GB  %4d  MB  %4d  KB  %4d  Byte ',
    [m_nTotalSendTB, m_nTotalSendGB, m_nTotalSendMB, m_nTotalSendKB, m_nTotalSendBytes ]);

    (*  发送速率(byte/s)    *)
    FillChar(buf, 1024, 0);
    LB_SendRate.Caption := ' ' + FormatFloat('#,#,#,#', m_SendRate);

    (*  接收速率(byte/s)    *)
    FillChar(buf, 1024, 0);
    LB_RecvRate.Caption := ' ' + FormatFloat('#,#,#,#', m_RecvRate);

    (* 系统运行时间         *)
    FillChar(buf, 1024, 0);
    LB_TimeRun.Caption := StrFmt(buf, ' %d 天  %d 时  %d 分  %d 秒 ',
    [m_nDayRun, m_nHourRun, m_nMinuteRun, m_nSecondRun ]);

    (* 历史最大连接数       *)
    LB_HistoryTime.Caption := ' '+m_MaxHistoryTime;
  end;
end;

end.
